﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace House_Control
{
    public partial class HouseControlForm : Form
    {
        public Log log;
        public Schedule appSchedule;
       

        public HouseControlForm()
        {
            InitializeComponent();

            Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
            controlTelevisionVolumeRm1.Text = "" + tv.getVolume();
            controlTelevisionChanRm1.Text = "" + tv.getChannel();

            Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
            controlDishwasherStatus.Text = dw.getApplianceStateString();
            controlDiswasherCycleTypeLbl.Text = "Dishwasher off";
            listBoxLog.DataSource = Program.log.getLog();

            refreshStatus();
            
        }
        //Various label states
        public void setAlert(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Red;
            lbl.Text = "Alert!";
        }
        public void setGood(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Lime;
            lbl.Text = "Good";
        }
        public void setArmed(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Lime;
            lbl.Text = "ARMED";
        }
        public void setDisarmed(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Red;
            lbl.Text = "DISARMED";
        }
        public void setRec(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Lime;
            lbl.Text = "Recording";
        }
        public void setStop(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Red;
            lbl.Text = "Stopped";
        }
        public void setInactive(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Red;
            lbl.Text = "Inactive";
        }
        public void setActive(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Lime;
            lbl.Text = "Active";
        }
        public void setTripped(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Red;
            lbl.Text = "TRIPPED!";
        }
        public void setBat(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Orange;
            lbl.Text = "BAT";
        }
        public void setOff(Label lbl)
        {
            lbl.BackColor = System.Drawing.Color.Gray;
            lbl.Text = "Off";
        }
        //Refresh status of all labels and indicators
        public void refreshStatus()
        {
            listBoxLog.DataSource = null;
            listBoxLog.DataSource = Program.log.getLog();
            listBoxLog.SetSelected(listBoxLog.Items.Count-1,true);
            Boolean totStat = true;
            Label[] labels = {labelFireStat, labelSmokeStat, labelMotionStat, labelDoorStat, labelWindowStat};
            int[] alarms = {Alarm.FIRE, Alarm.SMOKE, Alarm.MOTION, Alarm.DOOR, Alarm.WINDOW};
            for(int i=0;i<labels.Length; i++)
            {
                if (Program.house.isTripped(alarms[i]))
                {
                    setAlert(labels[i]);
                    totStat = false;
                }
                else if (Program.house.isArmed == false && labels[i]!=labelSmokeStat)
                    setOff(labels[i]);
                else
                    setGood(labels[i]);
            }
            
            //Set System Status Label
            if (totStat == false)
                setTripped(labelSysStat);
            else if (Program.house.isArmed)
                setArmed(labelSysStat);
            else
                setDisarmed(labelSysStat);

            //low smoke battery
            if (Program.house.lowSmokeBat())
                setBat(labelSmokeStat);

            //fire alarm tab
            if (Program.house.smokeAlarms[0].lowBat)
                setBat(labelSmoke1);
            else
                setGood(labelSmoke1);
            if (Program.house.smokeAlarms[1].lowBat)
                setBat(labelSmoke2);
            else
                setGood(labelSmoke2);
            labelSmoke1Alert.Text = Program.house.smokeAlarms[0].lastAlert;
            labelSmoke2Alert.Text = Program.house.smokeAlarms[1].lastAlert;
            progressBarBat1.Value = Program.house.smokeAlarms[0].getBatLife();
            labelBat1Percent.Text = Program.house.smokeAlarms[0].getBatLife().ToString() + "%";
            progressBarBat2.Value = Program.house.smokeAlarms[1].getBatLife();
            labelBat2Percent.Text = Program.house.smokeAlarms[1].getBatLife().ToString() + "%";
            //Survielance
            if (Program.house.survelliance.isOn())
            {
                setActive(labelDVRActive);
                buttonSvToggle.Text = "Turn Off Surveillance";
            }
            else
            {
                setInactive(labelDVRActive);
                buttonSvToggle.Text = "Turn On Surveillance";
            }

            if (Program.house.survelliance.cameras[0].enabled)
            {
                setRec(labelCam1Rec);
                pictureBoxCam1T.Image = House_Control.Properties.Resources.cam1;
                pictureBoxCam1.Image = House_Control.Properties.Resources.cam1;
                buttonCam1Toggle.Text = "Turn Off";
            }
            else
            {
                setStop(labelCam1Rec);
                pictureBoxCam1T.Image = House_Control.Properties.Resources.No_Stream;
                pictureBoxCam1.Image = House_Control.Properties.Resources.No_Stream;
                buttonCam1Toggle.Text = "Turn On";
            }
            if (Program.house.survelliance.cameras[1].enabled)
            {
                setRec(labelCam2Rec);
                pictureBoxCam2T.Image = House_Control.Properties.Resources.cam2;
                pictureBoxCam2.Image = House_Control.Properties.Resources.cam2;
                buttonCam2Toggle.Text = "Turn Off";
            }
            else
            {
                setStop(labelCam2Rec);
                pictureBoxCam2T.Image = House_Control.Properties.Resources.No_Stream;
                pictureBoxCam2.Image = House_Control.Properties.Resources.No_Stream;
                buttonCam2Toggle.Text = "Turn On";
            }
            if (Program.house.survelliance.cameras[2].enabled)
            {
                setRec(labelCam3Rec);
                pictureBoxCam3T.Image = House_Control.Properties.Resources.cam3;
                pictureBoxCam3.Image = House_Control.Properties.Resources.cam3;
                buttonCam3Toggle.Text = "Turn Off";
            }
            else
            {
                setStop(labelCam3Rec);
                pictureBoxCam3T.Image = House_Control.Properties.Resources.No_Stream;
                pictureBoxCam3.Image = House_Control.Properties.Resources.No_Stream;
                buttonCam3Toggle.Text = "Turn On";
            }

        }
        private void controlTelevisionChanUp_Click(object sender, EventArgs e)
        {
            Television tv = (Television) Program.house.getRoom(0).getAppliance(0);
            tv.incChannel();

            controlTelevisionChanRm1.Text = ""+tv.getChannel();
        }

        private void controlTelevisionChanDownRm1_Click(object sender, EventArgs e)
        {
            Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
            tv.decChannel();

            controlTelevisionChanRm1.Text = "" + tv.getChannel();
        }

        private void controlTelevisionVolUpRm1_Click(object sender, EventArgs e)
        {
            Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
            tv.incVolume();

            controlTelevisionVolumeRm1.Text = "" + tv.getVolume();
        }

        private void controlTelevisionVolDownRm1_Click(object sender, EventArgs e)
        {
            Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
            tv.decVolume();

            controlTelevisionVolumeRm1.Text = "" + tv.getVolume();
        }

        private void controllTelevisionRm1ChanButton_Click(object sender, EventArgs e)
        {
            Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
            try
            {
                tv.setChannel(int.Parse(controlTelevisionRm1ChanText.Text));
            }
            catch (System.Exception)
            {
                MessageBox.Show("Invalid entry!");
            }

            controlTelevisionChanRm1.Text = "" + tv.getChannel();
        }

        private void controllTelevisionRm1VolButton_Click(object sender, EventArgs e)
        {
            Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
            try
            {
                tv.setVolume(int.Parse(controlTelevisionRm1VolText.Text));
            }
            catch (System.Exception)
            {
                MessageBox.Show("Invalid entry!");
            }

            controlTelevisionVolumeRm1.Text = "" + tv.getVolume();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
            tv.changeApplianceState();

            controllTelevisionRm1OnOff.Text = tv.getApplianceStateString();
        }

        private void controlDishwasherOnOff_Click(object sender, EventArgs e)
        {
            Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
            dw.changeApplianceState();
            controlDishwasherStatus.Text = dw.getApplianceStateString();

            if (dw.getApplianceState()) controlDiswasherCycleTypeLbl.Text = dw.getCycleType();
            else controlDiswasherCycleTypeLbl.Text = "Dishwasher off";

            controlDishwasherOnOff.Text = dw.getApplianceStateString();
        }

        private void controlDishwasherCycleTypeCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
            dw.setCycleType(controlDishwasherCycleTypeCombo.Text);
        }

        private void controlDishwasherTurbo_CheckedChanged(object sender, EventArgs e)
        {
            Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
            dw.setTurboZone();
        }

        private void ccontrolDishwasherHigh_CheckedChanged(object sender, EventArgs e)
        {
            Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
            dw.setHighTemp();
        }

        private void controlDishwasherSani_CheckedChanged(object sender, EventArgs e)
        {
            Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
            dw.setSaniRinse();
        }

        private void controlDishwasherHeated_CheckedChanged(object sender, EventArgs e)
        {
            Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
            dw.setHeatedDry();
        }

        private void controlLightsUpR1L1_Click(object sender, EventArgs e)
        {
            Light l = (Light)Program.house.getRoom(0).getAppliance(2);
            l.brighten();

            controlLightsRm1BrightLbl.Text = ""+l.getBrightness();
        }

        private void controllLightsDownR1L1_Click(object sender, EventArgs e)
        {
            Light l = (Light)Program.house.getRoom(0).getAppliance(2);
            l.dim();

            controlLightsRm1BrightLbl.Text = "" + l.getBrightness();
        }

        private void controlLightsR1L1_Click(object sender, EventArgs e)
        {
            Light l = (Light)Program.house.getRoom(0).getAppliance(2);
            l.changeApplianceState();
            if (controlLightsR1L1.Text == "Turn Off")
            {
                l.setBrightness(0);
                controlLightsR1L1.Text = "Turn On";
            }
            else
            {
                l.setBrightness(100);
                controlLightsR1L1.Text="Turn Off";
            }
            controlLightsRm1BrightLbl.Text = "" + l.getBrightness();
        }

        private void controlLightsRm1BrightnessBut_Click(object sender, EventArgs e)
        {
            Light l = (Light)Program.house.getRoom(0).getAppliance(2);
            try
            {
                l.setBrightness(int.Parse(controlLightsBrightText.Text));
            }
            catch (System.Exception)
            {
                MessageBox.Show("Invalid entry!");
            }

            controlLightsRm1BrightLbl.Text = "" + l.getBrightness();
        }

        private void controlPetStationFoodBtn_Click(object sender, EventArgs e)
        {
            PetStation ps = (PetStation)Program.house.getRoom(0).getAppliance(3);
            ps.dispenseTreat();

            controlPetStationFoodLbl.Text = ""+ps.getFood();
        }

        private void controlPetStationWaterBtn_Click(object sender, EventArgs e)
        {
            PetStation ps = (PetStation)Program.house.getRoom(0).getAppliance(3);
            ps.dispenseWater();

            controlPetStationWaterLbl.Text = "" + ps.getWater();
        }

        private void controlPetStationCallBtn_Click(object sender, EventArgs e)
        {
            PetStation ps = (PetStation)Program.house.getRoom(0).getAppliance(3);
            ps.callPet();
        }

        private void controlPetStationVideoBtn_Click(object sender, EventArgs e)
        {
            PetStation ps = (PetStation)Program.house.getRoom(0).getAppliance(3);
            ps.changeApplianceState();

            if (ps.getApplianceState()) controlPetStationVideoStream.Image = House_Control.Properties.Resources.Rush_StairsB;
            else controlPetStationVideoStream.Image = House_Control.Properties.Resources.No_Stream;

            controlPetStationVideoBtn.Text = ps.getApplianceStateString();
            
        }

        private void controlOvenTargetBtn_Click(object sender, EventArgs e)
        {
            Oven o = (Oven)Program.house.getRoom(0).getAppliance(4);
            try
            {
                o.setTargetTemp(double.Parse(controlOvenTartgetTempText.Text));
            }
            catch (System.Exception)
            {
                MessageBox.Show("Invalid entry!");
            }

            controlOvenTargetLbl.Text = ""+o.getTargetTemp();
        }

        private void controlOvenOnOffBtn_Click(object sender, EventArgs e)
        {
            Oven o = (Oven)Program.house.getRoom(0).getAppliance(4);
            o.changeApplianceState();

            controlOvenTargetLbl.Text = "75.6";

            controlOvenOnOffBtn.Text = o.getApplianceStateString();
        }

        private void controlOvenLightOnOffBtn_Click(object sender, EventArgs e)
        {
            Oven o = (Oven)Program.house.getRoom(0).getAppliance(4);
            o.changeLightState();

            controlOvenLightOnOffBtn.Text = o.getLightStateString();
        }

        private void HouseControlForm_Load(object sender, EventArgs e)
        {

        }


        private void setButton_Click(object sender, EventArgs e)
        {
            if(appBox.Text=="TV"||appBox.Text=="Television"||appBox.Text=="television"||appBox.Text=="tv"){
                Television tv = (Television)Program.house.getRoom(0).getAppliance(0);
                appSchedule = new Schedule(tv, Convert.ToInt16(monthBox.Text),
                Convert.ToInt32(dayBox.Text),
                Convert.ToInt32(yearBox.Text),
                Convert.ToInt32(hourBox.Text),
                Convert.ToInt32(minBox.Text));
            }
            else if (appBox.Text == "Lights" || appBox.Text == "Light" || appBox.Text == "lights" || appBox.Text == "light")
            {
                Light l = (Light)Program.house.getRoom(0).getAppliance(2);
                appSchedule = new Schedule(l, Convert.ToInt16(monthBox.Text),
                Convert.ToInt32(dayBox.Text),
                Convert.ToInt32(yearBox.Text),
                Convert.ToInt32(hourBox.Text),
                Convert.ToInt32(minBox.Text));
                l.setButton(controlLightsR1L1);
            }
            else if (appBox.Text == "Dishwasher" || appBox.Text == "dishwasher")
            {
                Dishwasher dw = (Dishwasher)Program.house.getRoom(0).getAppliance(1);
                appSchedule = new Schedule(dw, Convert.ToInt16(monthBox.Text),
                Convert.ToInt32(dayBox.Text),
                Convert.ToInt32(yearBox.Text),
                Convert.ToInt32(hourBox.Text),
                Convert.ToInt32(minBox.Text));
            }
            else if (appBox.Text == "Pet Station" || appBox.Text == "pet station")
            {
                PetStation ps = (PetStation)Program.house.getRoom(0).getAppliance(3);
                appSchedule = new Schedule(ps, Convert.ToInt16(monthBox.Text),
                Convert.ToInt32(dayBox.Text),
                Convert.ToInt32(yearBox.Text),
                Convert.ToInt32(hourBox.Text),
                Convert.ToInt32(minBox.Text));
            }
            else
            {


                appSchedule = new Schedule(Convert.ToInt16(monthBox.Text),
                    Convert.ToInt32(dayBox.Text),
                   Convert.ToInt32(yearBox.Text),
                   Convert.ToInt32(hourBox.Text),
                    Convert.ToInt32(minBox.Text));
            }
            MessageBox.Show("Schedule set for " + appBox.Text + ".");
            Program.sc.setSchedule(appSchedule);
        }
      

        private void lightsSched_Click(object sender, EventArgs e)
        {
            controlTabControl.SelectedIndex = 6;
            appBox.Text = "Lights";
            
        }

   

        private void tvSched_Click(object sender, EventArgs e)
        {
            controlTabControl.SelectedIndex = 6;
            appBox.Text = "TV";
        }

        private void dishSched_Click(object sender, EventArgs e)
        {
            controlTabControl.SelectedIndex = 6;
            appBox.Text = "Dishwasher";
        }

        private void petSched_Click(object sender, EventArgs e)
        {
            controlTabControl.SelectedIndex = 6;
            appBox.Text = "Pet Station";
        }

        public Schedule getSchedule()
        {
            return appSchedule;
        }

        private void buttonF1_Click(object sender, EventArgs e)
        {
            Program.house.fireAlarms[0].trip();
            refreshStatus();
        }

        private void buttonF2_Click(object sender, EventArgs e)
        {
            Program.house.fireAlarms[1].trip();
            refreshStatus();
        }

        private void buttonS1_Click(object sender, EventArgs e)
        {
            Program.house.smokeAlarms[0].trip();
            refreshStatus();
        }

        private void buttonArmHouse_Click(object sender, EventArgs e)
        {
            Program.house.armHouse();
            refreshStatus();
        }

        private void buttonDisarmHouse_Click(object sender, EventArgs e)
        {
            Program.house.disarmHouse();
            refreshStatus();
        }

        private void buttonDeactivateAlarms_Click(object sender, EventArgs e)
        {
            Program.house.deactivateAlarms();
            refreshStatus();
        }

        private void buttonS2_Click(object sender, EventArgs e)
        {
            Program.house.smokeAlarms[1].trip();
            refreshStatus();
        }

        private void buttonM1_Click(object sender, EventArgs e)
        {
            Program.house.motionAlarms[0].trip();
            refreshStatus();
        }

        private void buttonM2_Click(object sender, EventArgs e)
        {
            Program.house.motionAlarms[1].trip();
            refreshStatus();
        }
        private void buttonD1_Click(object sender, EventArgs e)
        {
            Program.house.doorAlarms[0].trip();
            refreshStatus();
        }
        private void buttonD2_Click(object sender, EventArgs e)
        {
            Program.house.doorAlarms[1].trip();
            refreshStatus();
        }
        private void buttonW1_Click(object sender, EventArgs e)
        {
            Program.house.windowAlarms[0].trip();
            refreshStatus();
        }
        private void buttonW2_Click(object sender, EventArgs e)
        {
            Program.house.windowAlarms[1].trip();
            refreshStatus();
        }
        private void buttonB1_Click(object sender, EventArgs e)
        {
            Program.house.smokeAlarms[0].setBatLife(Int32.Parse(textBoxBat.Text));
            refreshStatus();
        }
        private void buttonB2_Click(object sender, EventArgs e)
        {
            Program.house.smokeAlarms[1].setBatLife(Int32.Parse(textBoxBat.Text));
            refreshStatus();
        }

        private void buttonSvToggle_Click(object sender, EventArgs e)
        {
            if (Program.house.survelliance.isOn())
            {
                Program.house.survelliance.stop();
                refreshStatus();
            }
            else
            {
                Program.house.survelliance.start();
                refreshStatus();
            }
        }

        private void buttonCam1Toggle_Click(object sender, EventArgs e)
        {
            if (Program.house.survelliance.cameras[0].enabled)
            {
                Program.house.survelliance.cameras[0].disable();
                refreshStatus();
            }
            else
            {
                Program.house.survelliance.cameras[0].enable();
                refreshStatus();
            }
        }

        private void buttonCam2Toggle_Click(object sender, EventArgs e)
        {
            if (Program.house.survelliance.cameras[1].enabled)
            {
                Program.house.survelliance.cameras[1].disable();
                refreshStatus();
            }
            else
            {
                Program.house.survelliance.cameras[1].enable();
                refreshStatus();
            }
        }

        private void buttonCam3Toggle_Click(object sender, EventArgs e)
        {
            if (Program.house.survelliance.cameras[2].enabled)
            {
                Program.house.survelliance.cameras[2].disable();
                refreshStatus();
            }
            else
            {
                Program.house.survelliance.cameras[2].enable();
                refreshStatus();
            }
        }

        private void pictureBoxCam1T_Click(object sender, EventArgs e)
        {
            surveillanceTabControl.SelectedTab = surveillanceCamera1;
        }

        private void pictureBoxCam2T_Click(object sender, EventArgs e)
        {
            surveillanceTabControl.SelectedTab = surveillanceCamera2;
        }

        private void pictureBoxCam3T_Click(object sender, EventArgs e)
        {
            surveillanceTabControl.SelectedTab = surveillanceCamera3;
        }
        private void controlPoolHeater_Click(object sender, EventArgs e)
        {
            Pool o = (Pool)Program.house.getRoom(2).getPool(0);
            try
            {
                o.setTargetTemp(double.Parse(PoolTargetTemp.Text));
            }
            catch (System.Exception)
            {
                MessageBox.Show("Invalid entry!");
            }

            TempLabel.Text = "" + o.getTargetTemp();
        }

        private void controlPoolLights_Click(object sender, EventArgs e)
        {
            Pool o = (Pool)Program.house.getRoom(2).getPool(0);
            o.changeLightState();

            controlPoolLights.Text = o.getLightStateString();
        }



        private void BathTubTemp_Click(object sender, EventArgs e)
        {
            Bathtub op = (Bathtub)Program.house.getRoom(2).getTub(0);
            try
            {
                op.setTargetTemp(double.Parse(BathTagetTemp.Text));
            }
            catch (System.Exception)
            {
                MessageBox.Show("Invalid entry!");
            }

            BathTubTargetTemp.Text = "" + op.getTargetTemp();
        }




        private void JetsOnOff_Click_1(object sender, EventArgs e)
        {
            Bathtub o = (Bathtub)Program.house.getRoom(2).getTub(0);
            o.changeJetState();

            JetsOnOff.Text = o.getJetStateString();
        }

        private void ChangeTemp_Click_1(object sender, EventArgs e)
        {
            Thermostat o = Program.house.getThermoStat();
            try
            {
                o.setTargetTemp(double.Parse(Temp.Text));
            }
            catch (System.Exception)
            {
                MessageBox.Show("Invalid entry!");
            }

            TempLlb.Text = "" + o.getTargetTemp() + "°F";
        }

        private void BlindsClosed_Click(object sender, EventArgs e)
        {
            Window o = (Window)Program.house.getRoom(2).getWindow(0);
            o.changeBlindsState();

            BlindsClosed.Text = o.getBlindStateString();
        }
    }
}
